package com.cxyzy.uidemos

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.RadioButton
import android.widget.SearchView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_common.*
import java.text.SimpleDateFormat

class CommonUiActivity : AppCompatActivity() {

    private lateinit var mContext: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        setContentView(R.layout.activity_common)
        setTextView()
        setEditText()
        setPwdEditText()
        setButton()
        setImageButton()
        setRadioButton()
        setRadioGroup()
        setCheckbox()
        setSwitch()
        setToggleButton()
        setImageView()
        setProgressBar()
        setHorizontalProgressBar()
        setSeekBar()
        setDiscreteSeekBar()
        setRatingBar()
        setSearchView()
        setCalendarView()
        setDatePicker()
    }

    private fun setTextView() {
        //设置文本
        textView.text = "程序园中猿"
        //设置文本颜色
        textView.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
    }

    private fun setEditText() {
        //设置输入框内容
        editText.setText("程序园中猿")
    }

    private fun setPwdEditText() {
        //设置密码输入框内容
        pwdEditText.setText("123456")
    }

    private fun setButton() {
        //设置按钮文字
        button.text = "Touch me"
        //设置点击事件
        button.setOnClickListener {
            Toast.makeText(this, "Feels good", Toast.LENGTH_SHORT).show()
        }
    }

    private fun setImageButton() {
        //设置按钮图片
        imageButton.setImageResource(R.mipmap.ic_launcher)
        //设置点击事件
        imageButton.setOnClickListener {
            Toast.makeText(this, "Go on", Toast.LENGTH_SHORT).show()
        }
    }

    private fun setRadioButton() {
        radioButton.text = "单选框333"
    }

    private fun setRadioGroup() {
        //获取选中的单选框
        val selectedRadioButton = findViewById<RadioButton>(radioGroup.checkedRadioButtonId)
        selectedRadioButton?.let {
            //获取选中单选框的文字
            val text = selectedRadioButton.text
        }
    }

    private fun setCheckbox() {
        //设置复选框显示文字
        checkBox.text = "复选框"
        //获取是否被选中
        val isChecked = checkBox.isChecked
    }

    private fun setSwitch() {
        //设置开关显示文字
        switch1.text = "开关"
        //获取是否被选中
        val isChecked = switch1.isChecked
    }

    private fun setToggleButton() {
        //获取是否被选中
        val isChecked = toggleButton.isChecked
    }

    private fun setImageView() {
        //修改图片
        imageView.setImageResource(R.mipmap.ic_launcher)
    }

    private fun setProgressBar() {
        //隐藏进度条
        progressBar.visibility = View.GONE
        //显示进度条
        progressBar.visibility = View.VISIBLE
    }

    private fun setHorizontalProgressBar() {
        //设置进度
        horizontalProgressBar.progress = 50
        //隐藏进度条
        horizontalProgressBar.visibility = View.GONE
        //显示进度条
        horizontalProgressBar.visibility = View.VISIBLE
    }

    private fun setSeekBar() {
        //设置进度
        seekBar.progress = 60
    }

    private fun setDiscreteSeekBar() {
        //设置进度
        discreteSeekBar.progress = 5
    }

    private fun setRatingBar() {
        //设置评分
        ratingBar.rating = 3.0f
    }

    private fun setSearchView() {
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            // 当点击搜索按钮时触发该方法
            override fun onQueryTextSubmit(query: String?): Boolean {
                Toast.makeText(mContext, query, Toast.LENGTH_SHORT).show()
                return false
            }

            // 当搜索内容改变时触发该方法
            override fun onQueryTextChange(newText: String?): Boolean {
                Toast.makeText(mContext, newText, Toast.LENGTH_SHORT).show()
                return false
            }
        })
    }

    private fun setCalendarView() {
        //设置初始日期
        val date = SimpleDateFormat("yyyy/MM/dd").parse("2020/02/02")
        date?.let { calendarView.date = it.time }

        //获取用户选择的日期
        calendarView.setOnDateChangeListener { calendarView, year, month, dayOfMonth ->
            Toast.makeText(mContext, "日期：${year}年${month + 1}月${dayOfMonth}日", Toast.LENGTH_SHORT)
                .show()
        }
    }

    private fun setDatePicker() {
        //设置初始日期,设置监听获取用户选择的日期
        datePicker.init(2020, 2, 2) { datePicker, year, month, dayOfMonth ->
            Toast.makeText(mContext, "日期：${year}年${month + 1}月${dayOfMonth}日", Toast.LENGTH_SHORT)
                .show()
        }
    }
}
